package class202205.demo25.demo23;


import java.util.Arrays;

class Node{
    public  int val;
    public Node left;
    public Node right;

    public Node (int val){
        this.val = val;
    }
}

public class BinarySearchTree {
    //二叉搜索树根节点
    public Node root = null;

    //查找
    public Node search (int key){
        Node cur = root;
        while (cur!=null) {

            if (cur.val == key) return cur;
            if (root.val < key) {
                cur = cur.left;
            } else {
                cur = cur.right;
            }
        }
        return null;//代表没有这个数据
    }

    //插入
    public boolean insert(int k) {
        if (root == null) {
            root = new Node(k);
            return true;
        }
        Node cur = root;
        Node parent = null;
        while (cur != null) {
            if (cur.val == k) return false;
            if (cur.val < k) {
                parent = cur;
                cur = cur.right;

            } else {
                parent = cur;
                cur = cur.left;
            }
        }
        if (parent.val > k) {
            parent.left = new Node(k);
        } else {
            parent.right = new Node(k);
        }
        return true;
    }
//删除
    public void remove(int key) {
        Node cur = root;
        Node parent = null;
        while (cur != null){
            if (cur.val == key){
                //这里开始删除
                removeNode(cur,parent);
                break;
            }else if (cur.val<key){
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    //代删
    public void removeNode(Node cur,Node parent){
        //判断搜索树的情况
        //1、
        if (cur.left == null){
            if (cur==root){
                root = cur.right;
            }else if (cur==parent.left){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
            //2、
        }else if (cur.right == null){
            if (cur == root){
                root = cur.left;
            }else if (cur==parent.left){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
            //3、
        }else {
            /*Node targetParent = cur;
            Node target = cur.right;
            while (target.left != null ){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if (target == targetParent.left){
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }*/
            //自我推测
            Node targetParent = cur;
            Node target = cur.left;
            while (target.right != null){
                targetParent = target;
                target = target.right;
            }
            cur.val = target.val;
            if (target == targetParent.right){
                targetParent.right = target.left;
            }else {
                targetParent.left = target.left;
            }
        }
    }

    //要测的话
    //中序
    public void inOrder(Node root){
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }



    public static void main(String[] args) {
        int[] array = {10,8,19,3,9,4,7};
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println("插入重复的数据");
        System.out.println(binarySearchTree.insert(3));
        binarySearchTree.remove(7);
        System.out.println();
        binarySearchTree.inOrder(binarySearchTree.root);
    }
}
